# -*- coding: utf-8 -*-
import os

# 设置日志级别
# os.environ["QT_LOGGING_RULES"] = "*.debug=false; *.info=false"

# 其他调用的类
import logging as log
import os
import time
import math

# Qt相关模块
from PySide6.QtCore import Slot, QThread, Qt, Signal, QPointF, QTimer


from PySide6.QtCore import (
    qDebug,
    qInfo,
    qWarning,
    qCritical,
    qFatal,
)
from PySide6.QtWidgets import QMainWindow, QFileDialog, QTableWidgetItem
from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis, QScatterSeries


# UI类
from ui_MainWindow import Ui_MainWindow

# 自定义类


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # 创建定时器，时间1秒
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.timeout)
        # self.timer.start(1000)

        self.tick = 0

        self.chart = QChart()  # 创建一个QChart对象
        self.chart.setTitle("数据曲线")

        self.ui.graphicsView.setChart(self.chart)

        self.testdata = [200, 50]

        self.axisX = QValueAxis()
        self.axisX.setRange(0, 100)  # 设置坐标轴范围
        self.axisX.setLabelFormat("%d")  # 标签格式
        self.axisX.setTickCount(5)  # 主分隔个数
        # axisX.setMinorTickCount(4)
        self.axisX.setTitleText("x")  # 标题
        # axisX.setGridLineVisible(True)
        self.axisX.setMinorGridLineVisible(False)

        self.axisY = QValueAxis()
        self.axisY.setRange(-2, 2)
        self.axisY.setLabelFormat("%d")  # 标签格式
        self.axisY.setTickCount(5)
        # axisY.setMinorTickCount(4)
        self.axisY.setTitleText("y")
        # axisY.setGridLineVisible(True)
        self.axisY.setMinorGridLineVisible(False)

        self.chart.addAxis(self.axisX, Qt.AlignBottom)
        self.chart.addAxis(self.axisY, Qt.AlignLeft)

        self.seriesS = QScatterSeries()
        self.seriesS.setMarkerSize(5)
        self.seriesL = QLineSeries()
        self.seriesS.setName("坐标点")
        self.seriesL.setName("拟合直线")
        self.chart.addSeries(self.seriesS)
        self.chart.addSeries(self.seriesL)
        self.chart.setAxisX(self.axisX, self.seriesS)  # 为序列设置坐标轴
        self.chart.setAxisY(self.axisY, self.seriesS)

        self.chart.setAxisX(self.axisX, self.seriesL)  # 为序列设置坐标轴
        self.chart.setAxisY(self.axisY, self.seriesL)

    @Slot()
    def on_pushButton_start_clicked(self):
        qDebug("debug start clicked")
        self.timer.start(100)

    @Slot()
    def on_pushButton_stop_clicked(self):
        qDebug("debug stop clicked")
        self.timer.stop()

    @Slot()
    def on_pushButton_clear_clicked(self):
        qDebug("debug clear clicked")
        self.seriesS.clear()
        self.seriesL.clear()

    def timeout(self):

        x = self.tick
        # x.append(sec)
        angle = math.radians(x * 10)
        y = math.sin(angle)
        # y.append(math.sin(sec))
        x_axis = x % 100
        self.seriesS.append(x_axis, y)
        self.seriesL.append(x_axis, y)
        self.tick += 1

        if self.tick % 100 == 0:
            self.seriesS.clear()
            self.seriesL.clear()
